package com.zx.rabbitmq.java.direct;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author Zx
 * @date 2021/4/18 14:35
 */
public class Consumer {

    private static Runnable runnable = new Runnable() {
        public void run() {
            // 所有的中间件技术都是基于tcp/IP协议的基础之上构建的, rabbitmq是用amqp
            // ip:port

            // 1 创建连接工程
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("193.193.50.110");
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("admin");
            connectionFactory.setVirtualHost("/");


            Connection connection = null;
            Channel channel = null;
            try {
                // 2 创建连接connection
                connection = connectionFactory.newConnection("消费者");
                // 3 通过连接获取通道channel
                channel = connection.createChannel();
                // 4 通过channel 创建交换机, 声明队列, 绑定关系, 路由key, 发送消息, 和接收消息
                final String queueName = Thread.currentThread().getName();
                /*
                 * @param1 队列名称
                 * @param2 是否持久化
                 * @param3 排他性, 是否是独占独立
                 * @param4 是否自动删除, 随着最后一个费者消费完毕消息以后是否把队列自动删除
                 * @param5 附属参数attachData
                 */
                channel.queueDeclare(queueName, false, false, false, null);

                // 5 接收消息
                channel.basicConsume(queueName, true, new DeliverCallback() {
                    public void handle(String consumerTag, Delivery message) throws IOException {
                        System.out.println(message.getEnvelope().getDeliveryTag());
                        System.out.println(queueName + "===" + new String(message.getBody(), "UTF-8"));
                    }
                }, new CancelCallback() {
                    public void handle(String consumerTag) throws IOException {
                        System.out.println("接收失败");
                    }
                });

                System.out.println("开始接收消息");
                System.in.read();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 6 关闭连接
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                // 7 关闭channel
                if (connection != null && connection.isOpen()) {
                    try {
                        connection.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    public static void main(String[] args) {
        new Thread(runnable, "queue1").start();
        new Thread(runnable, "queue2").start();
        new Thread(runnable, "queue3").start();
    }
}
